package com.shanghaionstar.activity.webview.handler;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;

import com.onstar.cn.common.jsBridge.BridgeWebView;
import com.shanghaionstar.R;
import com.shanghaionstar.nav.share.ShareDialog;
import com.shanghaionstar.utils.ApplicationUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Rengui Lu on 2017/3/3.
 */
public class LandingBannerHandler extends DefaultWebHandler {
    private static final String BANNER_CAN_SHARE = "1";
    private WebDataEntity entity;
    private String thumbnailUrl;
    private boolean canShare;
    private String titleStr = "";


    public LandingBannerHandler(Context context) {
        super(context);
    }

    @Override
    public void handMsg(Message msg) {
        switch (msg.what) {
            case IWebType.TYPE_BANNER:
                initBanner(msg);
                break;
            case IWebType.TYPE_ON_OVERRIDE_URL:
                handleOverUrl(msg);
                break;
            case IWebType.TYPE_ON_PAGE_FINISHED:
                onPageFinished();
                break;
            case IWebType.TYPE_ON_ACTIVITY_RESULT:
            case IWebType.TYPE_LANDING_BANNER:
                initBanner(msg);
                break;
        }
    }


    private void initBanner(Message msg) {
        if (context instanceof CommonWebViewActivity) {
            final CommonWebViewActivity mActivity = (CommonWebViewActivity) context;
            BridgeWebView webView = mActivity.getWebview();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
            }
            final WebDataEntity entity = loadUrl(msg, mActivity, webView);
            checkShare(mActivity, entity);

        }
    }

    private void checkShare(CommonWebViewActivity mActivity, final WebDataEntity entity) {
        if (canShare) {
            com.shanghaionstar.weight.TopLayout mTopLayout = mActivity.getTop_layout();
            mTopLayout.setOnRightTextClickLstener(mActivity.getString(R.string.share), new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showShareDialog(entity);
                }
            });
        }
    }

    @NonNull
    private WebDataEntity loadUrl(Message msg, CommonWebViewActivity mActivity, WebView webView) {
        Bundle bundle = mActivity.getBundle();
        thumbnailUrl = bundle.getString("thumbnailUrl");
        canShare = bundle.getBoolean("canShare");
        entity = bundle.getParcelable(IWebType.KEY_WEB_DATA);
        String accessToken = ApplicationUtil.getInstance().getToken();
        Map<String, String> extraHeaders = new HashMap<>();
        extraHeaders.put("Authorization", accessToken);
        webView.loadUrl(entity.getLinkUrl(), extraHeaders);
        return entity;
    }

    private void showShareDialog(WebDataEntity webDataEntity) {
        ShareDialog dialog = new ShareDialog(context, R.style.Theme_Transparent);
        dialog.setShareUrl(webDataEntity.getLinkUrl());
        if (!TextUtils.isEmpty(webDataEntity.getTopTitle())) {
            dialog.setShareTitle(webDataEntity.getTopTitle());
        } else {
            dialog.setShareTitle(titleStr);
        }
//        dialog.setShareDescription(mBannerEntity.getContent());
        dialog.setShareImageUrl(thumbnailUrl);
        dialog.show();
    }


    private void onPageFinished() {
        CommonWebViewActivity mActivity = (CommonWebViewActivity) context;
        WebView mWebView = mActivity.getWebview();
        String title = mWebView.getTitle();
        titleStr = title;
        if (!TextUtils.isEmpty(title)) {
            mActivity.getTop_layout().setVisibility(View.VISIBLE);
            mActivity.getTop_layout().setTopTitle(title);
        }
        if (canShare) {
            mActivity.getTop_layout().showRightButton(true);
        }
    }

    private boolean handleOverUrl(Message msg) {
        String url = (String) msg.obj;
        if (url.startsWith("http:") || url.startsWith("https:")) {
            return false;
        }
        try {
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            context.startActivity(intent);
        } catch (Exception e) {
        }
        return true;
    }


}
